﻿Imports CoreCode
Imports CoreCode.Connection

Public Class Sec_Module
    Private clsDBCon As New Connection
    Private sSQLAdapter(0) As System.Data.SqlClient.SqlDataAdapter
    Private sDataSet As New DataSet

    Private WithEvents sBindSource As New BindingSource

    Private lBinded As Boolean
    Private drv As DataRowView

    Private dtgName As String
    Private nBookMark As Single

    Public eAct As eAction

    Private Sub Sec_Role_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sSQLAdapter(0) = New System.Data.SqlClient.SqlDataAdapter
        nBookMark = 0
    End Sub

    Public Function TBConnect() As Boolean
        With clsDBCon
            .DatabaseName = .SecDatabaseName
            .DatabasePassword = .ConnectionPassword
            .DatabaseServer = .ConnectionServer
            .DatabaseServerType = ServerType.MSSQL
            .DatabaseUserName = .ConnectionUserName
            .Connect(.SQLCn)
            .DatabaseName = .SetDatabaseName
            .Connect(.SQLCnSet)
            .DatabaseName = .TranDatabaseName
            .Connect(.SQLCnTran)
            .DatabaseName = .SecDatabaseName
            Return .Connect(.SQLCnSec)
        End With
    End Function
    Public Function TBNew(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.NewRec
        TBNew = False
        Try
            With clsDBCon
                TBConnect()

                sDataSet.Tables.Clear()
                .DataUnbind(Me)
                .ObjectControl(Me, ObjectMethod.Cleardata)
                .ObjectControl(Me, ObjectMethod.ObjectLock, False)

                TBNew = True

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Function TBEdit(ByVal obj As Object) As Boolean
        obj.tag = "newedit"
        eAct = eAction.EditRec
        With clsDBCon
            TBEdit = False
            .ObjectControl(Me, ObjectMethod.ObjectLock, False)
            TBEdit = True
        End With
    End Function
    Public Function TBDelete(ByVal obj As Object) As Boolean
        With clsDBCon
            TBDelete = False
            If MsgBox("Are you sure you want to delete this item?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, TitleHeader) = MsgBoxResult.Yes Then
                .ExecuteNonQuery(.SQLCnSec, "UPDATE " & Me.Name & " SET lDelete=1 WHERE " & cModuleID.Name & "='" & cModuleID.Text & "'")

                obj.tag = "find"
                TBFind(obj)

                TBDelete = True

            End If
        End With
    End Function
    Public Function TBSave(ByVal obj As Object) As Boolean
        Try
            TBSave = False
            With clsDBCon
                .lKillRecursive = False
                If .MandatoryOK(Me) = False Then TBSave = False : Exit Function
                .ExecuteStoredProc(.SQLCnSec, "sp" & Me.Name, Me, eAct)
                Dim s As String = .GetSearchString(Me, True)
                nID.Text = .GetValueFrTable("nID", Me.Name, s)
                .ObjectControl(Me, ObjectMethod.ObjectLock, True)
                MsgBox(SaveMsg, MsgBoxStyle.Information, TitleHeader)

                TBSave = True

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Public Function TBUndo(ByVal obj As Object) As Boolean
        TBUndo = False
        With clsDBCon
            obj.tag = "clear"
            TBFind(obj)

            TBUndo = True

        End With
    End Function
    Public Function TBFind(ByVal obj As Object) As Boolean
        Try
            TBFind = False
            With (clsDBCon)
                TBConnect()
                If obj.tag = "clear" Or obj.tag = "" Then
                    .WaitWindow(WaitLoadingMsg, True)
                    obj.tag = "find"
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.ObjectLock, True)

                    Dim sSearch As String
                    sSearch = .GetSearchString(Me)
                    .OpenRecord(.SQLCnSec, sSQLAdapter(0), sDataSet, "*", Me.Name, "lDelete=0 " & IIf(sSearch = "", "", " and " & sSearch))

                    .DatagridPopup(dtgList, .SQLCnSec, Me.Name)

                    sBindSource.DataSource = sDataSet.Tables(Me.Name)
                    .Databind(Me, sBindSource)
                    sBindSource.Position = nBookMark
                    .WaitWindow("", False)
                Else
                    obj.tag = "clear"
                    .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                    .DataUnbind(Me)
                    sDataSet.Tables.Clear()
                    .ObjectControl(Me, ObjectMethod.Cleardata)

                End If
                TBFind = True
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Public Sub TBNewItem()

    End Sub
    Public Sub TBRemoveItem()

    End Sub
    Public Sub GetLocalChild()

    End Sub

    Private Sub bNavi_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles bNavi.ItemClicked
        Dim lRet As Boolean
        Select Case e.ClickedItem.Name
            Case "tsNew"
                lRet = TBNew(mode)
            Case "tsEdit"
                lRet = TBEdit(mode)
            Case "tsDelete"
                lRet = TBDelete(mode)
            Case "tsSave"
                lRet = TBSave(mode)
            Case "tsUndo"
                lRet = TBUndo(mode)
            Case "tsFind"
                lRet = TBFind(mode)
        End Select
        If lRet = True Then TBControl(e.ClickedItem)
    End Sub

    Public Sub TBControl(ByVal obj As Object)
        Select Case obj.name
            Case "tsNew", "tsEdit", "tsDelete", "tsSave", "tsUndo", "tsFind"
                tsNew.Enabled = False
                tsEdit.Enabled = False
                tsDelete.Enabled = False
                tsSave.Enabled = False
                tsUndo.Enabled = False
                tsFind.Enabled = False
                tsMoveFirst.Enabled = False
                tsMoveLast.Enabled = False
                tsMoveNext.Enabled = False
                tsMovePrev.Enabled = False
                tsCurrentPos.Enabled = False
        End Select

        Select Case obj.Name
            Case "tsNew"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsEdit"
                tsSave.Enabled = True
                tsUndo.Enabled = True
                mode.Tag = "newedit"
            Case "tsDelete"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
            Case "tsSave"
                tsNew.Enabled = True
                tsEdit.Enabled = True
                tsDelete.Enabled = True
                tsFind.Enabled = True
                tsCurrentPos.Enabled = True
                tsMoveFirst.Enabled = True
                tsMoveLast.Enabled = True
                tsMoveNext.Enabled = True
                tsMovePrev.Enabled = True
                mode.Tag = "find"
            Case "tsUndo"
                If mode.Tag = "new" Then
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                Else
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                End If
            Case "tsFind"
                If mode.Tag = "find" Then
                    tsNew.Enabled = True
                    tsEdit.Enabled = True
                    tsDelete.Enabled = True
                    tsFind.Enabled = True
                    tsCurrentPos.Enabled = True
                    tsMoveFirst.Enabled = True
                    tsMoveLast.Enabled = True
                    tsMoveNext.Enabled = True
                    tsMovePrev.Enabled = True
                    mode.Tag = "find"
                Else
                    tsNew.Enabled = True
                    tsFind.Enabled = True
                    mode.Tag = "clear"
                End If
        End Select
    End Sub

    Private Sub sBindSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sBindSource.PositionChanged
        If mode.Tag = "find" Then GetLocalChild()
        nBookMark = sBindSource.Position
    End Sub

    Private Sub btnPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPic.Click
        Try
            Dim f As New OpenFileDialog
            f.Filter = "Image Files (*.bmp,*.gif,*.jpg,*.png)|*.bmp;*.gif;*.jpg;*.png|All Files|*.*"
            f.FileName = cIcon.Text
            If f.ShowDialog = DialogResult.OK Then
                If Image.FromFile(f.FileName).PhysicalDimension.Height > 16 Or Image.FromFile(f.FileName).PhysicalDimension.Width > 16 Then
                    MsgBox("Image size is too large! " & vbNewLine & vbNewLine & _
                           "Allowed Height: 16px    Current: " & Image.FromFile(f.FileName).PhysicalDimension.Height & "px" & vbNewLine & _
                           "Allowed Width:  16px    Current: " & Image.FromFile(f.FileName).PhysicalDimension.Width & "px", MsgBoxStyle.Exclamation, "Navigator") : Exit Sub
                End If
                cIcon.Text = f.SafeFileName
                iIcon.Image = Image.FromFile(f.FileName)

            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "NavSystemStructure") : Exit Sub
        End Try
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        iIcon.Image = Nothing
        cIcon.Text = ""
    End Sub
 
    Private Sub btnParent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParent.Click
        TBConnect()
        Dim sResult As String = clsDBCon.PopupPicker(1, clsDBCon.SQLCnSet)
        If sResult = "" Then Exit Sub
        Dim s() As String = Split(Mid$(sResult, 2, Len(sResult) - 2), "▪")
        cParentID.Text = s(0)
        txtParentName.Text = s(1)
        s = Nothing
    End Sub

    Private Sub cParentID_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cParentID.TextChanged
        If txtParentName.Text = "" Then
            txtParentName.Text = clsDBCon.GetValueFrTable("cModuleName", Me.Name, "cModuleID='" & cParentID.Text & "'")
        End If
    End Sub
End Class
